package com.easyattendance.model;

import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="TBL_STUDENT")
@IdClass(StudentPK.class)
public class Student {
		
		
	/* One to many mappings of student */

	@OneToMany(mappedBy="student", fetch=FetchType.LAZY)
	@Cascade(value = {org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
	private Set<StudentElective> studentElective;
	
	@OneToMany(mappedBy="student", fetch=FetchType.LAZY)
	@Cascade(value = {org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
	private Set<AssignmentStudent> assignmentStudents;
	
	@OneToMany(mappedBy="student", fetch=FetchType.LAZY)
	@Cascade(value = {org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
	private Set<StudentAttendance> studentAttendances;
	
	@OneToMany(mappedBy="student", fetch=FetchType.LAZY)
	@Cascade(value = {org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
	private Set<StudentLeave> studentLeaves;
	
	/* many to one mappings of student */
	
	@ManyToOne
    @JoinColumn(name="section_id",insertable=false,updatable=false)
    private Section section;
	
	@ManyToOne
    @JoinColumn(name="class_id",insertable=false,updatable=false)
	@Cascade(value={org.hibernate.annotations.CascadeType.SAVE_UPDATE})
	private Class classObj;
	
	@ManyToOne
    @JoinColumn(name="parent_id")	
	private Parent parent;
	
	@Id
	@Column(name="stud_roll_no",length=3,nullable=false)
	private int stud_roll_no;
	
	@Column(name="stud_id",length=10)
	private String stud_id;
	
	
	
	@Column(name="stud_fname",length=32,nullable=false)
	private String stud_fname;
	
	@Column(name="stud_lname",length=32,nullable=false)
	private String stud_lname;
	
	@Column(name="stud_email_id",length=50,unique=true)
	private String stud_email_id;
	
	@Column(name="stud_address",length=100)
	private String stud_address;
	
	@Column(name="stud_home_no",length=10)
	private long stud_home_no;
	
	@Column(name="stud_mobile_no",length=10)
	private long stud_mobile_no;
	
	@Column(name="stud_gender",length=10)
	private String stud_gender;
	
	@Column(name="stud_dob")
	private Date stud_dob;

	/*@GenericGenerator(name="parent_id",strategy="foreign")
	@Column(name="parent_id")
	private String parent_id; */
	
	@GenericGenerator(name="section_id",strategy="foreign")
	@Column(name="section_id",length=10)
	private String section_id;
	
	@Id
	@GenericGenerator(name="class_id",strategy="foreign")
	@Column(name="class_id",length=15)
	private String class_id;
	
	public Student() {
		super();
	}

	public Student(String stud_fname, String stud_lname,
			String stud_email_id, String stud_address) {
		super();
		//this.stud_roll_no = stud_roll_no;
		this.stud_fname = stud_fname;
		this.stud_lname = stud_lname;
		this.stud_email_id = stud_email_id;
		this.stud_address = stud_address;
	}

	public Student(String stud_id, int stud_roll_no, String stud_fname, String stud_lname,
			String stud_email_id, String stud_address, long stud_home_no,
			long stud_mobile_no, String gender, Date dob, String section_id,
			String class_id) {
		super();
		this.stud_id = stud_id;
		this.stud_roll_no = stud_roll_no;
		this.stud_fname = stud_fname;
		this.stud_lname = stud_lname;
		this.stud_email_id = stud_email_id;
		this.stud_address = stud_address;
		this.stud_home_no = stud_home_no;
		this.stud_mobile_no = stud_mobile_no;
		this.stud_gender = gender;
		this.stud_dob = dob;
		this.section_id = section_id;
		this.class_id = class_id;
	}

	
	public Student(int stud_roll_no, String stud_fname, String stud_lname) {
		super();
		this.stud_roll_no = stud_roll_no;
		this.stud_fname = stud_fname;
		this.stud_lname = stud_lname;
	}

	public Set<StudentElective> getStudentElective() {
		return studentElective;
	}

	public void setStudentElective(Set<StudentElective> studentElective) {
		this.studentElective = studentElective;
	}

	public Set<AssignmentStudent> getAssignmentStudents() {
		return assignmentStudents;
	}

	public void setAssignmentStudents(Set<AssignmentStudent> assignmentStudents) {
		this.assignmentStudents = assignmentStudents;
	}

	public Set<StudentAttendance> getStudentAttendances() {
		return studentAttendances;
	}

	public void setStudentAttendances(Set<StudentAttendance> studentAttendances) {
		this.studentAttendances = studentAttendances;
	}

	public Section getSection() {
		return section;
	}

	public void setSection(Section section) {
		this.section = section;
	}

	public int getStud_roll_no() {
		return stud_roll_no;
	}

	public void setStud_roll_no(int stud_roll_no) {
		this.stud_roll_no = stud_roll_no;
	}

	public String getStud_id() {
		return stud_id;
	}

	public void setStud_id(String stud_id) {
		this.stud_id = stud_id;
	}

	public String getStud_fname() {
		return stud_fname;
	}

	public void setStud_fname(String stud_fname) {
		this.stud_fname = stud_fname;
	}

	public String getStud_lname() {
		return stud_lname;
	}

	public void setStud_lname(String stud_lname) {
		this.stud_lname = stud_lname;
	}

	public String getStud_email_id() {
		return stud_email_id;
	}

	public void setStud_email_id(String stud_email_id) {
		this.stud_email_id = stud_email_id;
	}

	public String getStud_address() {
		return stud_address;
	}

	public void setStud_address(String stud_address) {
		this.stud_address = stud_address;
	}

	public long getStud_home_no() {
		return stud_home_no;
	}

	public void setStud_home_no(long stud_home_no) {
		this.stud_home_no = stud_home_no;
	}

	public long getStud_mobile_no() {
		return stud_mobile_no;
	}

	public void setStud_mobile_no(long stud_mobile_no) {
		this.stud_mobile_no = stud_mobile_no;
	}

	public String getStud_gender() {
		return stud_gender;
	}

	public void setStud_gender(String stud_gender) {
		this.stud_gender = stud_gender;
	}

	public Date getStud_dob() {
		return stud_dob;
	}

	public void setStud_dob(Date stud_dob) {
		this.stud_dob = stud_dob;
	}

	public Class getClassObj() {
		return classObj;
	}

	public void setClassObj(Class classObj) {
		this.classObj = classObj;
	}

	public Parent getParent() {
		return parent;
	}

	public void setParent(Parent parent) {
		this.parent = parent;
	}

	public String getSection_id() {
		return section_id;
	}

	public void setSection_id(String section_id) {
		this.section_id = section_id;
	}

	public String getClass_id() {
		return class_id;
	}

	public void setClass_id(String class_id) {
		this.class_id = class_id;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((classObj == null) ? 0 : classObj.hashCode());
		result = prime * result
				+ ((class_id == null) ? 0 : class_id.hashCode());
		result = prime * result
				+ ((stud_email_id == null) ? 0 : stud_email_id.hashCode());
		result = prime * result + ((stud_id == null) ? 0 : stud_id.hashCode());
		result = prime * result + stud_roll_no;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		
		if (stud_email_id.equals(other.stud_email_id))
			return true;
		if (!stud_id.equals(other.stud_id))
			return true;
		if (stud_roll_no == other.stud_roll_no)
			return true;
		return true;
	}

	

}
